// pages/openRecordDetail/openRecordDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: {},
    categorys: [],
    enterprise_invoice_id: '',
    imgSrcArr: [],
    picSrcArr: [],
    taxSrcArr: [],
    tax1SrcArr: [],
    invoiceArr:[]
  },
  getInvoiceDetail(id) {
    let data = {
      enterprise_invoice_id: id
    }
    wx.showLoading()
    wx.$api.invoiceDetail(data).then(res => {
      wx.hideLoading();
      console.log(res.data)
      if (res.data.code == 200) {

        let {
          detail,
          categorys
        } = res.data.data;
        let imgSrcArr = [],
          picSrcArr = [],
          taxSrcArr = [],
          tax1SrcArr = [],
          invoiceArr =[];
        if (detail.contract_lists && detail.contract_lists.length > 0) {
          for (let item of detail.contract_lists) {
            picSrcArr.push(item.resource_href)
          }
        }
        if (detail.invoice_img_arr && detail.invoice_img_arr.length > 0) {
          for (let list1 of detail.invoice_img_arr) {
            invoiceArr.push(list1.resource_href)
          }
        }
        if (detail.collection_lists && detail.collection_lists.length > 0) {
          for (let list2 of detail.collection_lists) {
            imgSrcArr.push(list2.resource_href)
          }
        }
        if (detail.tax_payment_img1 && detail.tax_payment_img1.length > 0) {
          for (let list3 of detail.tax_payment_img1) {
            taxSrcArr.push(list3.resource_href)
          }
        }
        if (detail.tax_payment_img2 && detail.tax_payment_img2.length > 0) {
          for (let list4 of detail.tax_payment_img2) {
            tax1SrcArr.push(list4.resource_href)
          }
        }
        this.setData({
          detail:detail,
          categorys:categorys,
          picSrcArr:picSrcArr,
          imgSrcArr:imgSrcArr,
          taxSrcArr:taxSrcArr,
          tax1SrcArr:tax1SrcArr,
          invoiceArr:invoiceArr
        })
        console.log(this.data)
      }
    }).catch(ret => {
      wx.$Toast(ret.msg || "服务繁忙")
    })
  },
  previewImage(e) {
    console.log(this.data)
    let {
      index,
      type
    } = e.currentTarget.dataset, previewImg=[];
    if (type == 0) {
      previewImg = this.data.imgSrcArr
    } else if (type == 1) {
      previewImg = this.data.picSrcArr
    } else if (type == 2) {
      previewImg = this.data.taxSrcArr
    } else if (type == 3) {
      previewImg = this.data.tax1SrcArr
    }else if (type == 4) {
      previewImg = this.data.invoiceArr
    }
    wx.previewImage({
      urls: previewImg,
      current: previewImg[index],
      success: (res) => {
        console.log(res)
      },
      fail: (res) => {
        console.log(res)
      },
      complete: (res) => {
        console.log(res)
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      let id = options.id;
      console.log(options)
      console.log(id)
      this.setData({
        enterprise_invoice_id: id
      })
      this.getInvoiceDetail(id);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  goRecord() {
    let data = {
      enterprise_invoice_id: this.data.enterprise_invoice_id
    }
    wx.$api.confirmInvoice(data).then(res => {
      wx.hideLoading();
      console.log(res.data)
      if (res.data.code == 200) {
        wx.$Toast('提交成功！')
        setTimeout(() => {
          wx.reLaunch({
            url: '/pages/openRecord/openRecord',
          })
        }, 500)

      }
    }).catch(ret => {
      console.log(ret)
      wx.$Toast(ret.msg || "服务繁忙")
    })

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})